package com.wss.service.impl;

import com.wss.dao.PermissionDao;
import com.wss.domain.Message;
import com.wss.domain.Permission;
import com.wss.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("permissionService")
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    @Override
    public List<Permission> findAll() {
        List<Permission> list = permissionDao.findAll();
        return list;
    }

    @Override
    public boolean addPermission(Permission permission) {
        int res = permissionDao.addPermission(permission);
        if (res > 0) {
            return true;
        }else{
            return false;
        }
    }

    @Override
    public boolean updatePermission(Permission permission) {
        int res = permissionDao.updatePermission(permission);
        if (res > 0) {
            return true;
        }else{
            return false;
        }
    }

    /**
     * 删除前先检查权限是否有用户正在使用
     * @param p_id
     * @return
     */
    @Override
    public Message deletePermission(int p_id) {
        Message message = new Message();
        int p_idCheck = permissionDao.p_idCheck(p_id);
        if (p_idCheck > 0){
            message.setResultMsg("删除失败，此权限正在被使用");
        }else{
            int res = permissionDao.deletePermission(p_id);
            if (res > 0) {
                message.setResCode(1);
                message.setResultMsg("删除成功");
            }else{
                message.setResCode(0);
                message.setResultMsg("删除失败");
            }
        }
        return message;
    }
}
